var db=require("../db/db.js");
var express=require("express");
var router=require('vue-router')
var app=express();
var bodyParser=require("body-parser");
const { request, response } = require("express");
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
var router=express.Router();
// const myartical={
//     "title":"评论模块2",
//     "description":"我的评论模块"
// }
// new db.Artical(myartical).save()
//db.Artical({"title":"英文美句","description":"中英翻译"}).save()

db.Artical.find().then(res=>{
    //console.log(res)
}).catch(err=>{
    console.log(err)
})
router.get('/myBlog/myArtical',(request,response)=>{
    db.Artical.updateOne({_id:request.query._id},{$set:{title:request.query.title,description:request.query.description,articalContent:request.query.articalContent}}).then((res)=>{
        //console.log(res)
    })
    // console.log(request.query._id);
    // console.log(request.query.title)
    db.Artical.find({username:request.query.username}).then(res=>{
        //console.log("123")
        response.send(res)
    }).catch(err=>{
        console.log(err)
    })
})
router.get("/myBlog/edit",(request,response)=>{
    db.Artical.find(request.query).then(res=>{
        if(res!=""){
            response.send(res);
            //console.log(res)
        }
        else{
            console.log("未找到")
        }
       
    }).catch(err=>{
        console.log(err)
    })
})
router.get('/myBlog/delete',(request,response)=>{
    db.Artical.deleteOne(request.query).then(()=>{
        db.Artical.find({username:request.query.username}).then(res=>{

            response.send(res)
        }).catch((err)=>{
            console.log(err)
        }) 
    }).catch(err=>{
        console.log(err)
    })
})
router.get('/myBlog/add',(request,response)=>{
    new db.Artical(request.query).save()
})
router.get('/getArtical',(request,response)=>{

    db.Artical.find().then(res=>{
        let obj=[]
        res.forEach(item=>{
            if(item.articalContent!=undefined){
                
                if(item.articalContent.indexOf(request.query.info)>=0){
                    
                    obj.push(item)
                   
                    console.log(obj)
                }

                //console.log(item)
            }  
        })
        response.send(obj)
        //console.log(res[0])
    })
})
router.get("/getMyArtical",(request,response)=>{
    db.Artical.find({username:request.query.username}).then(res=>{
        let obj=[]
        res.forEach(item=>{
            if(item.articalContent!=undefined){
                
                if(item.articalContent.indexOf(request.query.info)>=0){
                    
                    obj.push(item)
                   
                    console.log(obj)
                }

                //console.log(item)
            }  
        })
        response.send(obj)
        //console.log(res[0])
    })
})
module.exports=router;